package com.tle.eventexample;

import java.text.DecimalFormat;
import java.util.ArrayList;

import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.tle.libs.CONSTANTS;
import com.tle.libs.CONSTANTS.FONT_PATHS;
import com.tle.libs.CONSTANTS.MARGINS;
import com.tle.libs.CONSTANTS.TEXT_SIZES;
import com.tle.libs.UIUtilities;

public class Calculator extends BaseActivity 
{
	////////////////////////////////////////////////////////////////////////////////////////////////////
	// Properties //
	////////////////////////////////////////////////////////////////////////////////////////////////////	
	protected LinearLayout m_mainLayout;
	
	protected TextView m_lblNumA;
	protected EditText m_txtNumA;
	
	protected TextView m_lblNumB;
	protected EditText m_txtNumB;
	
	protected TextView m_lblResult;
	
	protected RadioButton m_rdoAdd;
	protected RadioButton m_rdoSub;
	protected RadioButton m_rdoMul;
	protected RadioButton m_rdoDiv;	
	
	protected Button m_btnComeBack;
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
																						// Properties //
	////////////////////////////////////////////////////////////////////////////////////////////////////
	
	
	
	
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
	// Constructor and Support Method //
	////////////////////////////////////////////////////////////////////////////////////////////////////
	
	protected void setActivityTitle() 
	{
		this.setTitle(R.string.title_Calculator);
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		this.initGUI();
		UIUtilities.setUIStandard(m_mainLayout , FONT_PATHS.PLAINT , TEXT_SIZES.NORMAL);
		this.registerEvent();
	}
	
	protected void initGUI ()
	{
		LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		layoutParams.setMargins(MARGINS.M5.getValue(), 0, MARGINS.M5.getValue(), 0);
				
		m_mainLayout = new LinearLayout(this);
		this.setContentView(m_mainLayout);
		
		m_mainLayout.setOrientation(LinearLayout.VERTICAL);
		
		
		
		LayoutParams layoutParams1 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		layoutParams1.rightMargin = CONSTANTS.MARGINS.M5.getValue();
		
		LayoutParams layoutParams2 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		
		m_lblNumA = new TextView(this);
		m_lblNumA.setText(R.string.Calculator_NumA);
		
		m_txtNumA = new EditText(this);
		m_txtNumA.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
		
		m_mainLayout.addView( UIUtilities.buildLayoutWith2Cols(m_lblNumA, layoutParams1, m_txtNumA, layoutParams2) , layoutParams); 
		
		
		
		m_lblNumB = new TextView(this);
		m_lblNumB.setText(R.string.Calculator_NumB);	
		
		m_txtNumB = new EditText(this);
		m_txtNumB.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
		
		m_mainLayout.addView( UIUtilities.buildLayoutWith2Cols(m_lblNumB, layoutParams1, m_txtNumB, layoutParams2) , layoutParams );
		
		
		
		m_mainLayout.addView(this.buildRadioButtonsLayout() , layoutParams);
		
		
		
		m_lblResult = new TextView(this);
		m_lblResult.setBackgroundColor(Color.BLACK);
		m_lblResult.setTextColor(Color.WHITE);
		m_lblResult.setTextSize(30.f);
		m_lblResult.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
		m_lblResult.setHorizontalScrollBarEnabled(true);
		m_lblResult.setHorizontallyScrolling(true);
		
		m_mainLayout.addView(m_lblResult , layoutParams);
		
		
		
		m_btnComeBack = new Button(this);
		m_btnComeBack.setText(R.string.const_btnComeBack);
		layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		layoutParams.setMargins(MARGINS.M2.getValue(), 0, MARGINS.M2.getValue(), 0);
		m_mainLayout.addView(m_btnComeBack , layoutParams);
	}
	
	
	
	protected HorizontalScrollView buildRadioButtonsLayout ()
	{						
		m_rdoAdd = new RadioButton(this);
		m_rdoAdd.setText(R.string.Calculator_RdoAdd);
		m_rdoAdd.setId(0);
		
		m_rdoSub = new RadioButton(this);
		m_rdoSub.setText(R.string.Calculator_RdoSub);
		
		m_rdoMul = new RadioButton(this);
		m_rdoMul.setText(R.string.Calculator_RdoMul);
		
		m_rdoDiv = new RadioButton(this);
		m_rdoDiv.setText(R.string.Calculator_RdoDiv);
	
		ArrayList<RadioButton> arrRadioButtons = new ArrayList<>();
		arrRadioButtons.add(m_rdoAdd);
		arrRadioButtons.add(m_rdoSub);
		arrRadioButtons.add(m_rdoMul);
		arrRadioButtons.add(m_rdoDiv);
	
		RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, 
																		   LayoutParams.WRAP_CONTENT, 
																		   1);
		
		return UIUtilities.buildRadioButtons(arrRadioButtons, layoutParams, 0);
	}

	
	
	protected void registerEvent ()
	{
		TextboxEvent textboxEvent = new TextboxEvent();
		m_txtNumA.setOnKeyListener(textboxEvent);
		m_txtNumB.setOnKeyListener(textboxEvent);
		
		CheckedChangeEvent checkedChangeEvent = new CheckedChangeEvent();
		m_rdoAdd.setOnCheckedChangeListener(checkedChangeEvent);
		m_rdoSub.setOnCheckedChangeListener(checkedChangeEvent);
		m_rdoMul.setOnCheckedChangeListener(checkedChangeEvent);
		m_rdoDiv.setOnCheckedChangeListener(checkedChangeEvent);
		
		ButtonEvent buttonEvent = new ButtonEvent();
		m_btnComeBack.setOnClickListener(buttonEvent);
	}
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
																	// Constructor and Support Method //
	////////////////////////////////////////////////////////////////////////////////////////////////////
	
	
	
	
	
	////////////////////////////////////////////////////////////////////////////////////////////////////
	// Support class and methods //
	////////////////////////////////////////////////////////////////////////////////////////////////////
	private class TextboxEvent implements OnKeyListener
	{
		@Override
		public boolean onKey(View v, int keyCode, KeyEvent event) 
		{
			// TODO Auto-generated method stub
			
			if (event.getAction() == KeyEvent.ACTION_UP)
			{
				if (v == m_txtNumA || v == m_txtNumB)
				{
					calculate();
					return true;
				}
				
			}// end KeyEvent.ACTION_UP
			
			return false;
		}
	}
	
	private class CheckedChangeEvent implements OnCheckedChangeListener
	{

		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
		{
			// TODO Auto-generated method stub
			calculate();
		}
		
	}
	
	private class ButtonEvent implements OnClickListener
	{
		@Override
		public void onClick(View v) 
		{
			// TODO Auto-generated method stub
			if (v == m_btnComeBack)
			{
				finish();
			}// end m_btnComeBack
			
		}
	}
	
	
	
	
	
	protected void calculate ()
	{
		if(m_txtNumA.getText().toString().isEmpty() == true || m_txtNumB.getText().toString().isEmpty() == true)
		{
			m_lblResult.setText("");
			return;
		}
		
		boolean bNumAFail = (m_txtNumA.getText().toString().length() == 1) && 
							(m_txtNumA.getText().toString().equals("-") == true || m_txtNumA.getText().toString().equals(".") == true);
		
		boolean bNumBFail = (m_txtNumB.getText().toString().length() == 1) && 
							(m_txtNumB.getText().toString().equals("-") == true || m_txtNumB.getText().toString().equals(".") == true);
		if( bNumAFail == true || bNumBFail == true )
		{
			m_lblResult.setText("");
			return;
		}
			
		
		float nNumA = Float.parseFloat(m_txtNumA.getText().toString());
		float nNumB = Float.parseFloat(m_txtNumB.getText().toString());
		
		float nRes = Float.MIN_NORMAL;
		DecimalFormat decimalFormat = new DecimalFormat();
		decimalFormat.setMaximumFractionDigits(2);
		
		if (m_rdoAdd.isChecked() == true)
		{
			nRes = nNumA + nNumB;
			m_lblResult.setText( decimalFormat.format(nNumA) + " + " + decimalFormat.format(nNumB) + " = " + decimalFormat.format(nRes) );
		}
		else if (m_rdoSub.isChecked() == true)
		{
			nRes = nNumA - nNumB;
			m_lblResult.setText( decimalFormat.format(nNumA) + " - " + decimalFormat.format(nNumB) + " = " + decimalFormat.format(nRes) );
		}
		else if (m_rdoMul.isChecked() == true)
		{
			nRes = nNumA * nNumB;
			m_lblResult.setText( decimalFormat.format(nNumA) + " x " + decimalFormat.format(nNumB) + " = " + decimalFormat.format(nRes) );
		}
		else if (m_rdoDiv.isChecked() == true)
		{
			if (nNumB == 0)
				m_lblResult.setText(R.string.Calculator_ErrCannotDiv0);
			else
			{				
//				decimalFormat.setMaximumFractionDigits(2);
				nRes = nNumA / nNumB;
				m_lblResult.setText( decimalFormat.format(nNumA) + " : " + decimalFormat.format(nNumB) + " = " + decimalFormat.format(nRes) );
			}
				
		}
		
	}
	////////////////////////////////////////////////////////////////////////////////////////////////////
																		 // Support class and methods //
	////////////////////////////////////////////////////////////////////////////////////////////////////
}
